package com.mp.chart.renderer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.mp.chart.animation.ChartAnimator;
import com.mp.chart.buffer.BollBodyBuffer;
import com.mp.chart.buffer.BollShadowBuffer;
import com.mp.chart.data.BollData;
import com.mp.chart.data.BollDataSet;
import com.mp.chart.data.BollEntry;
import com.mp.chart.data.Entry;
import com.mp.chart.interfaces.BollDataProvider;
import com.mp.chart.utils.ColorTemplate;
import com.mp.chart.utils.Highlight;
import com.mp.chart.utils.Transformer;
import com.mp.chart.utils.ViewPortHandler;

import java.util.List;


public class BollChartRenderer extends DataRenderer {


    protected BollDataProvider mChart;

    private BollShadowBuffer[] mShadowBuffers;
    private BollBodyBuffer[] mBodyBuffers;

    public BollChartRenderer(BollDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(animator, viewPortHandler);

        mChart = chart;


        mRenderPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    public void initBuffers() {
        BollData sarData = mChart.getBollData();
        final int dataSetCount = sarData.getDataSetCount();
        mShadowBuffers = new BollShadowBuffer[dataSetCount];
        mBodyBuffers = new BollBodyBuffer[dataSetCount];
        for (int i = 0; i < dataSetCount; i++) {
            BollDataSet set = sarData.getDataSetByIndex(i);
            mShadowBuffers[i] = new BollShadowBuffer(set.getValueCount() * 4);
            mBodyBuffers[i] = new BollBodyBuffer(set.getValueCount() * 4);
        }
    }

    @Override
    public void drawData(Canvas c) {
        BollData sarData = mChart.getBollData();

        for (BollDataSet set : sarData.getDataSets()) {

            if (set.isVisible())
                drawDataSet(c, set);
        }
    }

    private void drawDataSet(Canvas c, BollDataSet dataSet) {

        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        int dataSetIndex = mChart.getBollData().getIndexOfDataSet(dataSet);

        List<BollEntry> entries = dataSet.getYVals();

        Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
        Entry entryTo = dataSet.getEntryForXIndex(mMaxX);

        int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0);
        int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size());

        int range = (maxx - minx) * 4;
        int to = (int) Math.ceil((maxx - minx) * phaseX + minx);

        BollShadowBuffer shadowBuffer = mShadowBuffers[dataSetIndex];
        shadowBuffer.setPhases(phaseX, phaseY);
        shadowBuffer.limitFrom(minx);
        shadowBuffer.limitTo(maxx);
        shadowBuffer.feed(entries);

        trans.pointValuesToPixel(shadowBuffer.buffer);

        mRenderPaint.setStyle(Paint.Style.STROKE);

        // If not set, use default functionality for backward compatibility
        if (dataSet.getColor() == ColorTemplate.COLOR_NONE) {
            mRenderPaint.setColor(Color.argb(255, 41, 49, 109));
        } else {
            mRenderPaint.setColor(dataSet.getColor());
        }

        mRenderPaint.setStrokeWidth(dataSet.getStrokeWidth());
//        mCirclePaint.setmStrokeWidth(dataSet.getShadowWidth());
        // draw the shadow
//        c.drawLines(shadowBuffer.buffer, 0, range, mRenderPaint);

        BollBodyBuffer bodyBuffer = mBodyBuffers[dataSetIndex];
        bodyBuffer.setBodySpace(dataSet.getBodySpace());
        bodyBuffer.setPhases(phaseX, phaseY);
        bodyBuffer.limitFrom(minx);
        bodyBuffer.limitTo(maxx);
        bodyBuffer.feed(entries);

        trans.pointValuesToPixel(bodyBuffer.buffer);


        // draw the body
        for (int j = 0, i = 0; j < range; j += 4, i += 2) {

            // get the entry
            BollEntry e = entries.get(j / 4 + minx);

            if (!fitsBounds(e.getXIndex(), mMinX, to))
                continue;

            float leftBody = bodyBuffer.buffer[j];
            float open = bodyBuffer.buffer[j + 1];
            float rightBody = bodyBuffer.buffer[j + 2];
            float close = bodyBuffer.buffer[j + 3];

            float highX = shadowBuffer.buffer[j];
            float highY = shadowBuffer.buffer[j + 1];

            float lowX = shadowBuffer.buffer[j + 2];
            float lowY = shadowBuffer.buffer[j + 3];

            float center = (leftBody + rightBody) / 2;

            float[] lines = {
                    center, open, rightBody, open,
                    center, close, leftBody, close
            };


            if (open == close && highY == lowY && open == highY) {
                mRenderPaint.setColor(dataSet.getIncreasingColor());
                mRenderPaint.setStyle(Paint.Style.STROKE);
            } else if (open > close) { // decreasing
                mRenderPaint.setColor(dataSet.getDecreasingColor());
                c.drawLine(highX, highY, lowX, lowY, mRenderPaint);
            } else if (open < close) {
                mRenderPaint.setColor(dataSet.getIncreasingColor());
                c.drawLine(highX, highY, lowX, lowY, mRenderPaint);
            } else { // equal values
                mRenderPaint.setColor(dataSet.getIncreasingColor());
                c.drawLine(highX, highY, lowX, lowY, mRenderPaint);

            }
            c.drawLines(lines, mRenderPaint);

        }
    }

    @Override
    public void drawValues(Canvas c) {

    }

    @Override
    public void drawExtras(Canvas c) {

    }

    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {

        for (int i = 0; i < indices.length; i++) {

            int xIndex = indices[i].getXIndex(); // get the
            // x-position

            BollDataSet set = mChart.getBollData().getDataSetByIndex(
                    indices[i].getDataSetIndex());

            if (set == null || !set.isHighlightEnabled())
                continue;

            mHighlightPaint.setColor(set.getHighLightColor());

            BollEntry e = set.getEntryForXIndex(xIndex);

            if (e == null || e.getXIndex() != xIndex)
                continue;

//            float low = e.getLow() * mAnimator.getPhaseY();
//            float high = e.getHigh() * mAnimator.getPhaseY();

            float min = mChart.getYChartMin();
            float max = mChart.getYChartMax();

            float[] vertPts = new float[]{
                    xIndex - 0.5f, max, xIndex - 0.5f, min, xIndex + 0.5f, max, xIndex + 0.5f,
                    min
            };


            mChart.getTransformer(set.getAxisDependency()).pointValuesToPixel(vertPts);


            c.drawLine((vertPts[0] + vertPts[4]) / 2, vertPts[1], (vertPts[2] + vertPts[6]) / 2, vertPts[3], mHighlightPaint);

        }
    }
}
